﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS.BL;
using SES.CMS.DO;

namespace SES.CMS.AdminCP
{
    public partial class Images : System.Web.UI.Page
    {
        MediaDO objMedia = new MediaDO();
        cmsAlbumDO objAlbum = new cmsAlbumDO();
        static bool insert = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Functions.ddlDatabinder(ddlProduct, cmsProductDO.PRODUCTID_FIELD, cmsProductDO.PRODUCTTITLE_FIELD, new cmsProductBL().SelectAll());
                Functions.ddlDatabinder(ddlAlbum, cmsAlbumDO.ALBUMID_FIELD, cmsAlbumDO.TITLE_FIELD, new cmsAlbumBL().SelectAll());
                initForm();
            }
           
        }

        private void initForm()
        {
            if (Request.QueryString["ImagesID"] != null)
            {
                objMedia.MediaID = Convert.ToInt32(Request.QueryString["ImagesID"].ToString());
            //    Session["ID"] = Convert.ToInt32(Request.QueryString["ImagesID"].ToString());
                objMedia = new MediaBL().Select(objMedia);
                txtTitle.Text = objMedia.Title;
                txtDetail.Text = objMedia.Description;
                bool Type = objMedia.MediaType;
               
                try
                {
                    ddlAlbum.SelectedValue = objMedia.AlbumID.ToString();
                    ddlProduct.SelectedValue = objMedia.ProductID.ToString();
                    txtOrder.Text = objMedia.OrderID.ToString();
                }
                catch
                {

                    throw;
                }
                if (Type==false)
                {
                    trVideo.Visible = true;
                    trHinhAnh.Visible = false;
                }
                else
                {
                    trVideo.Visible = false;
                    trHinhAnh.Visible = true;
                }
                
                if (objMedia.MediaType == true) // Image - dung MediaFile
                {
                    rdImage.Checked = true;
                    if (!string.IsNullOrEmpty(objMedia.MediaFile))
                    {
                        hplImage.Visible = true;
                        hplImage.NavigateUrl = "/Media/" + objMedia.MediaFile;
                    }
                }
                else // Video - Dung MediaUrl
                {
                    rdVideo.Checked = true;
                    txtVideo.Text = "http://youtu.be/" + objMedia.MediaUrl;
                }
                cbIsHomePage.Checked = objMedia.IsHomePage;

            }
            else
            {
                objMedia = new MediaBL().Select(objMedia);
                txtTitle.Text = objMedia.Title;
                txtDetail.Text = objMedia.Description;
                //if (txtVideo.Text == "")
                //{
                //    trVideo.Visible = false;
                //}
                //else
                //{
                //    trVideo.Visible = true;
                //}
                //if (hplImage.Text == "")
                //{
                //    trHinhAnh.Visible = false;
                //}
                //else
                //{
                //    trHinhAnh.Visible = true;
                //}
                try
                {
                    ddlAlbum.SelectedValue = objMedia.AlbumID.ToString();
                    ddlProduct.SelectedValue = objMedia.ProductID.ToString();
                    txtOrder.Text = objMedia.OrderID.ToString();
                }
                catch
                {

                    throw;
                }
                if (objMedia.MediaType == true) // Image - dung MediaFile
                {
                    rdImage.Checked = true;
                    if (!string.IsNullOrEmpty(objMedia.MediaFile))
                    {
                        hplImage.Visible = true;
                        hplImage.NavigateUrl = "/Media/" + objMedia.MediaFile;
                    }
                }
                else // Video - Dung MediaUrl
                {
                    rdVideo.Checked = true;
                    txtVideo.Text = "http://youtu.be/" + objMedia.MediaUrl;
                }
                cbIsHomePage.Checked = objMedia.IsHomePage;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            MediaBL mediaBL = new MediaBL();
            
            initObject();

            if (objMedia.MediaID <=0)
            {
                int mediaID = mediaBL.Insert(objMedia);
                if (cbIsHomePage.Checked)
                    mediaBL.UpdateHomePageVideo(mediaID);
            }
            else
            {
                //objMedia.MediaID = Convert.ToInt32(Session["ID"].ToString());
                mediaBL.Update(objMedia);
                if (cbIsHomePage.Checked)
                    mediaBL.UpdateHomePageVideo(objMedia.MediaID);
            }
            if(txtVideo.Text!="")
            Functions.Alert("Cập nhật Video thành công!", "/Admincp/ListVideo.aspx");
            else
                Functions.Alert("Cập nhật Hình ảnh thành công!", "/Admincp/ListImages.aspx");
        }
        private void initObject()
        {
           // objMedia.MediaID = Convert.ToInt32(Session["ID"].ToString());
            if (!string.IsNullOrEmpty(Request.QueryString["ImagesID"]))
            {
                objMedia.MediaID = int.Parse(Request.QueryString["ImagesID"]);
            }
            objMedia = new MediaBL().Select(objMedia);
            objMedia.Title = txtTitle.Text;
            objMedia.Description = txtDetail.Text;
            objMedia.AlbumID = int.Parse(ddlAlbum.SelectedValue);
            objMedia.ProductID = int.Parse(ddlProduct.SelectedValue);
            try
            {
                objMedia.OrderID = int.Parse(txtOrder.Text.Trim());

                if (objMedia.MediaType==true)
                {
                    if (!string.IsNullOrEmpty(fuImage.FileName))
                    {
                        objMedia.MediaFile = UploadFile(fuImage);
                    }

                }
                else if (objMedia.MediaType == false)
                {

                    string s = txtVideo.Text;
                    objMedia.MediaUrl = s.Substring(s.LastIndexOf("/") + 1);
                }
            }
            catch
            {
                throw;
            }
            if (objMedia.MediaType == false)
                objMedia.MediaType = false;
            
            else
                objMedia.MediaType = true;
            objMedia.IsHomePage = cbIsHomePage.Checked;
            objMedia.DateUpload = DateTime.Now;
        }
        private string UploadFile(FileUpload fulImages)
        {
            if (!string.IsNullOrEmpty(fulImages.FileName))
            {
                string FileName = string.Format("{0}{1}", Functions.Change_AV(txtTitle.Text) + "-" + DateTime.Now.ToString("ddMMyyyyhhmmss"), fulImages.FileName.Substring(fulImages.FileName.LastIndexOf(".")));
                string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), FileName);
                fulImages.SaveAs(SaveLocation);
                return FileName;
            }
            return string.Empty;
        }

        internal static object FromFile(string imageFile)
        {
            throw new NotImplementedException();
        }

        public double Width { get; set; }

        public double Height { get; set; }

        protected void ddlAlbum_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = int.Parse(ddlAlbum.SelectedValue);
            objAlbum.AlbumID=id;
            objAlbum = new cmsAlbumBL().Select(objAlbum);
            bool Type = (bool)objAlbum.Type;
            if (Type == true)
            {
                trVideo.Visible = false;
                trHinhAnh.Visible = true;
                ddlAlbum.SelectedIndex = id;
            }
            else if (Type == false)
            {
                trVideo.Visible = true;
                trHinhAnh.Visible = false;
                ddlAlbum.SelectedIndex = id;
            }
            else
            {
                trVideo.Visible = false;
                trHinhAnh.Visible = false;
            }
            
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            int id = int.Parse(ddlAlbum.SelectedValue);
            objAlbum.AlbumID = id;
            objAlbum = new cmsAlbumBL().Select(objAlbum);
            bool Type = (bool)objAlbum.Type;
            if (Type == true)
            {

                Response.Redirect("ListImages.aspx");
            }
            else if (Type == false)
            {
                Response.Redirect("ListVideo.aspx");
            }
        }
    }
}